package com.zhangyu.shadow.repair.delegate.history.detail;

import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.HistoryDataBean;
import com.zhangyu.shadow.repair.intercept.DataIntercept;
import com.zhangyu.shadow.repair.manager.AccountManager;

import io.reactivex.disposables.Disposable;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * @author zhangyu
 * @date 2018/12/5
 */

public class HistoryDetailModel implements HistoryDetailContract.Model {

    @Override
    public Disposable getHistoryData(String id, String type, final IStringDataConvert<HistoryDataBean> success, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_GET_HISTORY_DATA)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("searchId", id)
                .params("busVmType", type)
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        success.convert(convertHistoryBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    private HistoryDataBean convertHistoryBean(String response) {

        return new ObjConverter<HistoryDataBean>().convert(response
                , new TypeReference<HistoryDataBean>() {
                });
    }

}
